Public Class ExDataGridView
    Inherits System.Windows.Forms.DataGridView

    Private _ObscuredColumns As New List(Of Integer)()

    Public Sub AddToObscuredColumns(ByVal ColumnIndex As Integer)
        If Not _ObscuredColumns.Contains(ColumnIndex) Then
            _ObscuredColumns.Add(ColumnIndex)
            Refresh()
        End If
    End Sub

    Public Sub AddToObscuredColumns(ByVal ArrayColumnIndex() As Integer)
        Dim IsNewItem As Boolean = False
        If ArrayColumnIndex.Length > 0 Then
            For i As Integer = 0 To ArrayColumnIndex.Length - 1
                If Not _ObscuredColumns.Contains(ArrayColumnIndex(i)) Then
                    _ObscuredColumns.Add(ArrayColumnIndex(i))
                    IsNewItem = True
                End If
            Next
            If IsNewItem Then
                Refresh()
            End If
        End If
    End Sub

    Public Sub RemoveFromObscuredColumns(ByVal ColumnIndex As Integer)
        If _ObscuredColumns.Contains(ColumnIndex) Then
            _ObscuredColumns.Remove(ColumnIndex)
            Refresh()
        End If
    End Sub

    Public Sub ClearObscuredColumns()
        If _ObscuredColumns.Count > 0 Then
            _ObscuredColumns.Clear()
            Refresh()
        End If
    End Sub

    Public Sub New()
        Me.Font = New Font("Arial", 10, FontStyle.Regular)
        Me.BackgroundColor = Color.White
        Me.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.AutoGenerateColumns = True
        Dim HeaderCellStyle As System.Windows.Forms.DataGridViewCellStyle = Me.ColumnHeadersDefaultCellStyle
        HeaderCellStyle.ForeColor = Color.White
        Me.ColumnHeadersDefaultCellStyle = HeaderCellStyle
        Me.RowHeadersDefaultCellStyle = HeaderCellStyle
        Me.EnableHeadersVisualStyles = False
    End Sub

    Protected Overrides Sub OnCellPainting(ByVal e As DataGridViewCellPaintingEventArgs)
        If e.RowIndex = -1 Then 'Draw a column header
            Dim objColumnHeaderBrush As New Drawing2D.LinearGradientBrush(New Rectangle(1, 1, e.CellBounds.Width, e.CellBounds.Height), _
                                        Color.FromArgb(182, 184, 184), Color.FromArgb(76, 76, 76), Drawing2D.LinearGradientMode.Vertical)
            Dim objColumnHeaderPen As Pen = New Pen(Color.FromArgb(76, 76, 76), 1)
            e.Graphics.FillRectangle(objColumnHeaderBrush, e.CellBounds)
            e.Graphics.DrawRectangle(objColumnHeaderPen, New Rectangle(e.CellBounds.X - 1, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height))
            e.Paint(e.ClipBounds, (DataGridViewPaintParts.All And Not DataGridViewPaintParts.Background))
            e.PaintContent(e.ClipBounds)
            e.Handled = True
        Else
            If e.ColumnIndex = -1 Then  'Draw a row header
                Dim objRowHeaderBrush As New Drawing2D.LinearGradientBrush(New Rectangle(1, 1, e.CellBounds.Width, e.CellBounds.Height), _
                                            Color.FromArgb(84, 84, 84), Color.FromArgb(84, 84, 84), Drawing2D.LinearGradientMode.Vertical)
                Dim objRowHeaderPen As Pen = New Pen(Color.FromArgb(208, 215, 229), 0.5)
                e.Graphics.FillRectangle(objRowHeaderBrush, e.CellBounds)
                e.Graphics.DrawRectangle(objRowHeaderPen, New Rectangle(e.CellBounds.X, e.CellBounds.Y - 1, e.CellBounds.Width, e.CellBounds.Height))
                e.PaintContent(e.ClipBounds)
                e.Paint(e.ClipBounds, (DataGridViewPaintParts.All And Not DataGridViewPaintParts.Background))
                e.Handled = True
            Else    'Draw a cell   
                If _ObscuredColumns.Contains(e.ColumnIndex) Then
                    Dim objRowHeaderPen As Pen = New Pen(Color.FromArgb(172, 168, 153), 0.5)
                    e.Graphics.FillRectangle(Brushes.Silver, e.CellBounds)
                    e.Graphics.DrawRectangle(objRowHeaderPen, New Rectangle(e.CellBounds.X - 1, e.CellBounds.Y - 1, e.CellBounds.Width, e.CellBounds.Height))
                    e.Handled = True
                Else
                    If Me.CurrentCell IsNot Nothing AndAlso e.RowIndex = Me.CurrentCell.RowIndex And e.CellStyle.BackColor <> Me.DefaultCellStyle.BackColor Then
                        e.Graphics.FillRectangle(New Pen(Me.Rows(e.RowIndex).DefaultCellStyle.BackColor).Brush, New Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width - 1, e.CellBounds.Height))
                        e.PaintContent(e.CellBounds)
                        e.Handled = True
                    End If
                End If
            End If
            e.Graphics.DrawLine(New Pen(Color.FromArgb(172, 168, 153), 1), New Point(e.CellBounds.X - 1, e.CellBounds.Y), New Point(e.CellBounds.X - 1, e.CellBounds.Y + e.CellBounds.Height - 1))
            e.Graphics.DrawLine(New Pen(Color.FromArgb(172, 168, 153), 1), New Point(e.CellBounds.X - 1, e.CellBounds.Y + e.CellBounds.Height - 1), New Point(e.CellBounds.X - 1 + e.CellBounds.Width, e.CellBounds.Y + e.CellBounds.Height - 1))
            e.Graphics.DrawLine(New Pen(Color.FromArgb(172, 168, 153), 1), New Point(e.CellBounds.X - 1 + e.CellBounds.Width, e.CellBounds.Y + e.CellBounds.Height - 1), New Point(e.CellBounds.X - 1 + e.CellBounds.Width, e.CellBounds.Y))
    End If

    If e.RowIndex = -1 AndAlso e.ColumnIndex = -1 Then
      e.Graphics.DrawString("#", Me.Font, Brushes.White, e.CellBounds.X + 15, e.CellBounds.Y + 3)
    End If

    End Sub

    Private Sub ExDataGridView_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles Me.RowPostPaint
        If Me.CurrentCell IsNot Nothing Then
            If e.RowIndex = Me.CurrentCell.RowIndex Then
                Dim SelectedBound As Rectangle = e.RowBounds
                If Me.RowHeadersVisible Then
                    SelectedBound.X = Me.RowHeadersWidth + 1
                    SelectedBound.Width -= Me.RowHeadersWidth
                End If
                SelectedBound.Width -= 1
                SelectedBound.Height -= 1
                e.Graphics.DrawRectangle(Pens.Orange, SelectedBound)
            End If
        End If
    End Sub

End Class

